tic;
    clear all;
    clc    
    noOfNodes = 310;        %The total number of nodes.
    R = 250;                % Maximum transmission range of the nodes;
    noOFpakets = 100000;   % Number of packets to send.
    networkSize =2200;      %Simulator in a 2200 m /2200 m network area.    
    finishingTime = 0;
    previousBuffer = 1;
    stopSimulation = 0;    
    trafficBuffer = zeros(1,2);    
    counter = 1;
    numberOfMoveNodes = 1;
    primaryPathUpdated = 0;
    finishMovePackets  = 1; 
    rand('state', 0);
    f = 1;
    figure(f);
    clf;
    hold on;       
     [velocity,thetaBuffer] = getThetaSpeed(noOfNodes);
     [disp_loc,matrix]=drawTheNodes(noOfNodes,networkSize,R);          
     [connected] = checkConnectedGraph(disp_loc, matrix);
     if connected == 1
         disp('That graph number');
         disp(f);
         disp('Is connected');
     else
         disp('That graph number');
         disp(f);
         disp('Is not connected');         
     end
     
     [source ,destination]=Sour_Dest(noOfNodes);%To choose both the source and destination
    for i = 1:1
        [primaryPathTable, backupPathTable, path] = calculatePrimaryBackupPath (source ,destination, disp_loc, matrix);
        
        if path ~= 0
            pr = length(primaryPathTable); 
            for j = 1:pr
                trafficBuffer(j,1) = primaryPathTable(j);
            end        
            while finishingTime < 100  && stopSimulation == 0      
                % for creating a traffic with certain number of packets, which was decided above. 
                [trafficBuffer] = creatTraffic (noOFpakets, trafficBuffer, primaryPathTable);                
                time = toc;
                disp('The time before the simulation :');
                disp(time);
                % for moving the traffic among the nodes include at the path from the source to the destination. 
                [finishMovePackets, primarySender, primaryReceiver, primaryPath, trafficBuffer, disp_loc, matrix, f] =  movePackets (trafficBuffer, primaryPathTable, finishingTime, disp_loc, R, noOfNodes,networkSize,velocity, thetaBuffer, f, matrix);
               time = toc;
               t = time - finishingTime;
               finishingTime = time;
               if t > 2 && primaryPath ~= 0 && path ~= 0
                    % Calling the function will move all the nodes in different direction as well as in random distance.                             
                    %We will move the nodes and recalculate the path and the backup path if there is a need to that.                
                    f = f + 1;
                    figure(f); 
                    clf;
                    hold on                 
                    [disp_loc, matrix, thetaBuffer] = moveNodes(noOfNodes,networkSize,R,disp_loc, velocity, thetaBuffer);
                    % Check the primary path if still the path is available. 
                    [primaryPath, primarySender, primaryReceiver] = checkingAvailabilityOfPath(primaryPathTable, disp_loc, R);
                        disp('Primary: The senders nodes are not connected with next hops.');
                        disp(primarySender);
                        disp('Primary: The receivers nodes are not connected with previous hops.');
                        disp(primaryReceiver);
               end
               if primaryPath ~= 0 && path ~= 0 
                    r = 0;
                    g = 0;
                    b = 1;
                    drawPath(primaryPathTable, disp_loc, r, g, b);                        
%                     if primaryPathTable(2) ~= backupPathTable(2) && primaryPathUpdated == 0
%                         r = 1;
%                         g = 0;
%                         b = 0;
%                         drawPath(backupPathTable, disp_loc, r, g, b);
%                     end
                    [trafficBuffer] = creatTraffic (noOFpakets, trafficBuffer, primaryPathTable); 
                    [finishMovePackets, primarySender, primaryReceiver, primaryPath, trafficBuffer, disp_loc, matrix, f] =  movePackets (trafficBuffer, primaryPathTable, finishingTime, disp_loc, R, noOfNodes,networkSize,velocity, thetaBuffer, f, matrix); 
                      
%                elseif path ~= 0
%                    disp('There is no path at the primary path table.');
%                     [newPath]= addBackupNodesToPrimary(primaryPathTable, backupPathTable, primarySender, primaryReceiver);
%                     NP = length(newPath);   
%                     node = newPath(NP);    
%                     if node ~= destination 
%                         nextH = NP + 1;
%                         newPath(nextH) = destination;
%                     end
%                     [backupPath, backupSender, backupReceiver] = checkingAvailabilityOfPath(newPath, disp_loc, R);
%                     disp('Backup: The senders nodes are not connected with next hops.');
%                     disp(backupSender);
%                     disp('Backup: The receivers nodes are not connected with previous hops.');
%                     disp(backupReceiver);
%                     if backupPath ~= 0
%                         PR1 = length(primaryPathTable);
%                         primaryPathTable = zeros(1,1);
%                         primaryPathTable = newPath;
%                         PR2 = length(primaryPathTable);
%                         if PR1 ~= PR2
%                             [trafficBuffer] = changeTrafficBuffer (primaryPathTable, trafficBuffer);
%                         end                        
%                         primaryPathUpdated = 1;
%                         r = 0;
%                         g = 0;
%                         b = 1;
%                         drawPath(primaryPathTable, disp_loc, r, g, b);
%                         k1 = length(trafficBuffer);
%                         if finishMovePackets == 0
%                             [finishMovePackets, primarySender, primaryReceiver, primaryPath, trafficBuffer, disp_loc, matrix, f] =  movePackets (trafficBuffer, primaryPathTable, finishingTime, disp_loc, R, noOfNodes,networkSize,velocity, thetaBuffer, f, matrix); 
%                             if finishMovePackets == 0                    
%                                 dest = length(primaryReceiver); 
%                                 if source == primarySender(1) || destination == primaryReceiver(dest);
%                                     disp('There is no possible path between the source and the destination.');
%                                     path = 0;
%                                 end
%                              end
%                         else
%                             [trafficBuffer] = creatTraffic (noOFpakets, trafficBuffer, primaryPathTable); 
%                             [finishMovePackets, primarySender, primaryReceiver, primaryPath, trafficBuffer, disp_loc, matrix, f] =  movePackets (trafficBuffer, primaryPathTable, finishingTime, disp_loc, R, noOfNodes,networkSize,velocity, thetaBuffer, f, matrix); 
%                         end
                    else
                        disp('There is no path at the backup path table also.');
                        stopSimulation = 1;
                        disp('Both [Primary/Backup] paths are broken you must re-calculate new path.');
                        [primaryPathTable, backupPathTable, path] = calculatePrimaryBackupPath (source ,destination, disp_loc, matrix);
                        if path ~= 0
                            [trafficBuffer] = changeTrafficBuffer (primaryPathTable, trafficBuffer);
                            stopSimulation = 0;
                        else
                            disp('There is no more path available between the source and the desination.');
                        end
%                     end  
                 end
            end
        end
    end
toc;